// pages/activity/good/good.js
import {
  getProdDetail
} from '../../../api/user'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [],
    productId: '',
    mallId: '',
    benifitMoney: 0,
    price: 0,
    rateDis: '',
    prodName: '',
    sellNum: 0,
    remainNum: 0,
    contentStr: '',
    navList: [{
      id: 0,
      name: '商品',
    }, {
      id: 1,
      name: '评价',
    }, {
      id: 2,
      name: '详情'
    }],
    activeId: 0,
    activeCurrent: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.productId = options.id;
    // this.mallId = options.mallId;
    this.setData({
      productId: options.id,
      mallId: options.mallId,
    })
    this.initProdDetail();
    // this.judgeCanShare();
  },

  initProdDetail() {
    getProdDetail(this.data.productId, this.data.mallId).then((res) => {
      const newData = {}
      console.log('商品详情', res);
      newData.benifitMoney = (res.data.product.rate && res.data.product.price) ? (res.data.product
        .rate * 100) * res.data.product.price / 100 : 0;
      newData.price = res.data.product.price ? res.data.product.price : 0;
      newData.rateDis = res.data.product.rate ? `${res.data.product.rate * 100}%` : '0%';
      newData.prodName = res.data.product.name;
      newData.sellNum = res.data.product.saleCount;
      newData.remainNum = res.data.product.stockCount;
      let detail = res.data.product.detail;
      let handleDetail = detail.replace(/<img/g, `<img style="width:100%;" class="aw"`)
      newData.contentStr = handleDetail;
      let tmpPic;
      if (res.data.product.pic.indexOf(',') > -1) {
        tmpPic = res.data.product.pic.split(',');
      } else {
        tmpPic = [res.data.product.pic];
      }
      newData.swiperList = tmpPic;
      this.setData(newData)
    });
  },
  chooseNav(e) {
    const {
      id
    } = e.currentTarget.dataset
    if (id === 1) {
      wx.showToast({
        title: '程序员玩命开发中!',
        icon: 'none'
      });
      return;
    }
    this.activeId = id;
    if (this.activeId === 2) {
      wx.pageScrollTo({
        scrollTop: 300,
        duration: 10
      });
    } else if (this.activeId === 0) {
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 10
      });
    }
  },
  shareProduct(){
    const userId = wx.getStorageSync('userId')
    if(!userId){
      wx.showToast({
        title: '请先登录',
        icon: 'none',
      })
      return 
    }
    const path = `/pages/good/good?tenantId=${this.data.mallId}&shareId=${userId}&productId=${this.data.productId}`
    console.log(path)
    wx.navigateToMiniProgram({
      appId: 'wx7239c7a2397fb19c',
      path: path,
      extraData: {
        // splid: id,
        // userId,
        // feedback:true,
      },
      envVersion: "release", // 打开正式版
      success(res) {
        // 打开成功
      },
      fail: function (err) {
      }
    })
  }
})